﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageConfigtable.Queries
{
    /// <summary>
    /// 列表
    /// </summary>
    public class GetconfigtableListQuery : IRequest<Json<PaginatedList<GetconfigtableDTO>>>
    {
        /// <summary>
        /// 第几页
        /// </summary>
        public int pageIndex { get; set; }
        /// <summary>
        /// 页码
        /// </summary>
        public int pageSize { get; set; }
    }

    public class GetconfigtableListQueryHandler : IRequestHandler<GetconfigtableListQuery, Json<PaginatedList<GetconfigtableDTO>>>
    {

        private readonly ICurrentUserService _icurrentUserServices;
        private readonly IConfigtableRepository _iconfigtableRepository;
        public GetconfigtableListQueryHandler(ICurrentUserService icurrentUserServices, IConfigtableRepository iconfigtableRepository)
        {
            _icurrentUserServices = icurrentUserServices;
            _iconfigtableRepository = iconfigtableRepository;
        }
        public async Task<Json<PaginatedList<GetconfigtableDTO>>> Handle(GetconfigtableListQuery request, CancellationToken cancellationToken)
        {
            PaginatedList<GetconfigtableDTO> model = new PaginatedList<GetconfigtableDTO>(new List<GetconfigtableDTO>() { }, 0, 0, 0);
            int userid = _icurrentUserServices.GetUserId();
            if (request.pageIndex <= 0 || request.pageSize > 1000)
            {
                return Json<PaginatedList<GetconfigtableDTO>>.Failed("参数不能为空", model);
            }
            else {
                model = await _iconfigtableRepository.GetconfigtableList(userid, request.pageIndex, request.pageSize, cancellationToken);
                return Json<PaginatedList<GetconfigtableDTO>>.Success("查询成功", model);
            }
        }
    }
}
